﻿using MengLin.OOP.DesignPattern.Observer.TearcherPhoneChange.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.Observer.TearcherPhoneChange.Implements
{
    /// <summary>
    /// 主题
    /// 具体的被观察者
    /// </summary>
    public class Teacher: ISubject
    {
        private ArrayList _container;//观察者集合列表(这里特指学生列表，家长列表)
        private string _phone;

        public Teacher(string phone)
        {
            _phone = phone;
        }

        public Teacher()
        {
            _container = new ArrayList();
        }

        public string Phone
        {
            get { return _phone; }
            set
            {
                _phone = value;
                //通知观察者
                NotifyObserver();
            }
        }

        /// <summary>
        /// 注册观察者
        /// </summary>
        /// <param name="obj"></param>
        public void RegisterObserver(IObserver obj)
        {
            _container.Add(obj);
        }

        /// <summary>
        /// 移除观察者
        /// </summary>
        /// <param name="obj"></param>
        public void UnRegisterObserver(IObserver obj)
        {
            _container.Remove(obj);
        }

        /// <summary>
        /// 通知观察者
        /// </summary>
        public void NotifyObserver()
        {
            //遍历学生集合
            for (int i = 0; i < _container.Count; i++)
            {
                ((IObserver)_container[i]).Update(Phone);
            }
        }
    }
}
